/*
 * The MIT License
 *
 * Copyright 2012 http://generic-toolset.googlecode.com.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.rlpw.swing.table;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.TableCellEditor;

/**
 *
 */
public class ColorEditor extends AbstractCellEditor implements TableCellEditor {

    private JPanel panel = new JPanel(new BorderLayout());
    private JColorChooser colorChooser;
    private Color currentColor;
    private JDialog dialog;
    private ColorRenderer renderer = new ColorRenderer();
    private static String title = "Pick a Color";
    private static Image iconImage;

    public ColorEditor() {

        final JButton button = new JButton(new javax.swing.plaf.metal.MetalComboBoxIcon());

        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dialog.setTitle(getTitle());
                dialog.setIconImage(iconImage);
                colorChooser.setColor(currentColor);
                dialog.setVisible(true);
                fireEditingStopped();
            }
        });
        button.setBorderPainted(false);
        colorChooser = new JColorChooser();
        dialog = JColorChooser.createDialog(button, getTitle(), true, colorChooser, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                currentColor = colorChooser.getColor();
            }
        }, null);

        panel.add("East", button);

    }

    @Override
    public Object getCellEditorValue() {
        return currentColor;
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {

        currentColor = (Color) value;
        JLabel label = (JLabel) renderer.getTableCellRendererComponent(table, value, isSelected, isSelected, row, column);
        label.setOpaque(false);

        panel.add("Center", label);

        panel.setOpaque(true);

        if (isSelected) {
            panel.setForeground(table.getSelectionForeground());
            panel.setBackground(table.getSelectionBackground());
        } else {

            Color aColor = UIManager.getColor("Table.alternateRowColor");

            if (row % 2 == 1 || aColor == null) {
                panel.setBackground(aColor);
            } else {
                panel.setOpaque(false);
                panel.setBackground(table.getBackground());
            }

        }

        return panel;
    }

    /**
     * Gets the title of the dialog. The title is displayed in the
     * dialog's border.
     * @return the title of this dialog window. The title may be <code>null</code>.
     * @see #setTitle
     */
    public static String getTitle() {
        return title;
    }

    /**
     * Sets the title of the Dialog.
     * @param title the title displayed in the dialog's border;
     * a  <code>null</code> value results in an empty title
     * @see #getTitle
     */
    public static void setTitle(String title) {
        ColorEditor.title = title;
    }

    /**
     * Sets the image to be displayed as the icon for the Dialog.
     * @param iconImage the icon image to be displayed.
     */
    public static void setIconImage(Image iconImage) {
        ColorEditor.iconImage = iconImage;
    }
}